package com.inspiration.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspiration.dao.TagMapper;
import com.inspiration.entity.Page;
import com.inspiration.entity.Tag;
import com.inspiration.entity.vo.TagVO;
import com.inspiration.utils.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description
 * @Author Vincent.jiao
 * @Date 2023/11/6 10:35
 */
@Service
public class TagService {
    @Autowired
    TagMapper tagMapper;

    public boolean insert(Tag tag) {
        tag.setT_name(tag.getT_name().trim());
        tag.setT_desc(tag.getT_desc().trim());

        int count = tagMapper.selectCount(new LambdaQueryWrapper<Tag>().eq(Tag::getT_name, tag.getT_name()));
        if(count > 1){
            return false ;
        }

        tagMapper.insert(tag);
        return true;
    }


    public Object batchInsert2(Tag[] tags) {
        if(tags == null || tags.length == 0) {
            return "ok";
        }

        for (int i = 0; i < tags.length; i++) {
            insert(tags[i]);
        }

        return "ok";
    }

    public List<TagVO> listVO(String tName) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper();
        if(StrUtil.isNotEmpty(tName)) {
            tName = tName.trim();
            queryWrapper.like(Tag::getT_name, tName);
            queryWrapper.or().like(Tag::getT_desc, tName);
        }

        return tagToVO(tagMapper.selectList(queryWrapper));
    }

    public Page page(String tName, String sortorder, int pageNo, int pageSize, String sortname ) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper();
        if(StrUtil.isNotEmpty(tName)) {
            tName = tName.trim();
            queryWrapper.like(Tag::getT_name, tName);
            queryWrapper.or().like(Tag::getT_desc, tName);
        }

        PageHelper.startPage(pageNo, pageSize, sortname +" "+ sortorder);
        PageInfo pageInfo = new PageInfo(tagMapper.selectList(queryWrapper));
        pageInfo.setList(tagToVO(pageInfo.getList()));

        return new Page(pageInfo);
    }

    public List<TagVO> tagToVO(List<Tag> list) {
        List<TagVO> result = new LinkedList<>();
        for (Tag item : list) {
            TagVO tagVO = new TagVO();
            BeanUtil.copyProperties(item, tagVO);
            tagVO.setContentCount( tagMapper.contentCount(item.getT_id()));
            result.add(tagVO);
        }
        return result;
    }

    public int contentCount(Long tId){
        return  tagMapper.contentCount(tId);
    }

    public int countAll() {
        return tagMapper.selectCount(new LambdaQueryWrapper<>());
    }

    public List<Tag> list(LambdaQueryWrapper queryWrapper) {
        return  tagMapper.selectList(queryWrapper);
    }
}
